﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter25
{
    using System.Threading;

    static partial class Program
    {
        internal static void Begin(Action action, bool active = false)
        {
            if (active)
            {
                Console.WriteLine("开始演示：");
                action();
                Console.WriteLine("演示完毕：");
                Console.ReadLine();
            }
        }

        internal static void C07()
        {
            Console.WriteLine("Main thread: starting a dedicated thread to do an asynchronous operation");
            var dedicatedThread = new Thread(ComputeBoundOp);
            dedicatedThread.Start(5);
            Console.WriteLine("Main thread: Doing other work here...");
            Thread.Sleep(10000);
            dedicatedThread.Join();
            Console.WriteLine("Hit <Enter> to this program...");
        }

        private static void ComputeBoundOp(object obj)
        {
            Console.WriteLine("In ComputeBoundOp: State = {0}", obj);
            Thread.Sleep(1000);
        }

        internal static void C10()
        {
            var t = new Thread(Worker);
            t.IsBackground = true;
            t.Start();
            Console.WriteLine("Returning from Main");
        }

        private static void Worker()
        {
            Thread.Sleep(10000);
            Console.WriteLine("Retruning from Worker");
        }
    }
}
